| Conditions | 1 |
| Paths | 1 |
| Total Lines | 83 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /* |
||
| 23 | app.factory('StackService', function(ApiService, $http, $q){ |
||
| 24 | var StackService = function($http, ep, $q) { |
||
| 25 | ApiService.call(this, $http, ep, $q); |
||
| 26 | }; |
||
| 27 | StackService.prototype = angular.copy(ApiService.prototype); |
||
| 28 | StackService.prototype.fetchAll = function(boardId) { |
||
| 29 | var deferred = $q.defer(); |
||
| 30 | var self=this; |
||
| 31 | $http.get(this.baseUrl +'/'+boardId).then(function (response) { |
||
| 32 | self.clear(); |
||
| 33 | self.addAll(response.data); |
||
| 34 | deferred.resolve(self.data); |
||
| 35 | }, function (error) { |
||
|
|
|||
| 36 | deferred.reject('Error while loading stacks'); |
||
| 37 | }); |
||
| 38 | return deferred.promise; |
||
| 39 | }; |
||
| 40 | |||
| 41 | StackService.prototype.fetchArchived = function(boardId) { |
||
| 42 | var deferred = $q.defer(); |
||
| 43 | var self=this; |
||
| 44 | $http.get(this.baseUrl +'/'+boardId+'/archived').then(function (response) { |
||
| 45 | self.clear(); |
||
| 46 | self.addAll(response.data); |
||
| 47 | deferred.resolve(self.data); |
||
| 48 | }, function (error) { |
||
| 49 | deferred.reject('Error while loading stacks'); |
||
| 50 | }); |
||
| 51 | return deferred.promise; |
||
| 52 | }; |
||
| 53 | |||
| 54 | StackService.prototype.addCard = function(entity) { |
||
| 55 | if(!this.data[entity.stackId].cards) { |
||
| 56 | this.data[entity.stackId].cards = []; |
||
| 57 | } |
||
| 58 | this.data[entity.stackId].cards.push(entity); |
||
| 59 | }; |
||
| 60 | |||
| 61 | StackService.prototype.reorder = function(entity, order) { |
||
| 62 | // assign new order |
||
| 63 | for(var i=0, j=0;i<this.data[entity.stackId].cards.length;i++) { |
||
| 64 | if(this.data[entity.stackId].cards[i].id === entity.id) { |
||
| 65 | this.data[entity.stackId].cards[i].order = order; |
||
| 66 | } |
||
| 67 | if(j === order) { |
||
| 68 | j++; |
||
| 69 | } |
||
| 70 | if(this.data[entity.stackId].cards[i].id !== entity.id) { |
||
| 71 | this.data[entity.stackId].cards[i].order = j++; |
||
| 72 | } |
||
| 73 | } |
||
| 74 | // sort array by order |
||
| 75 | this.data[entity.stackId].cards.sort(function(a,b) { |
||
| 76 | if (a.order < b.order) |
||
| 77 | return -1; |
||
| 78 | if (a.order > b.order) |
||
| 79 | return 1; |
||
| 80 | return 0; |
||
| 81 | }); |
||
| 82 | }; |
||
| 83 | |||
| 84 | StackService.prototype.updateCard = function(entity) { |
||
| 85 | var self = this; |
||
| 86 | var cards = this.data[entity.stackId].cards; |
||
| 87 | for(var i=0;i<cards.length;i++) { |
||
| 88 | if(cards[i].id == entity.id) { |
||
| 89 | cards[i] = entity; |
||
| 90 | } |
||
| 91 | } |
||
| 92 | }; |
||
| 93 | StackService.prototype.removeCard = function(entity) { |
||
| 94 | var self = this; |
||
| 95 | var cards = this.data[entity.stackId].cards; |
||
| 96 | for(var i=0;i<cards.length;i++) { |
||
| 97 | if(cards[i].id == entity.id) { |
||
| 98 | cards.splice(i, 1); |
||
| 99 | } |
||
| 100 | } |
||
| 101 | }; |
||
| 102 | |||
| 103 | service = new StackService($http, 'stacks', $q); |
||
| 104 | return service; |
||
| 105 | }); |
||
| 106 | |||
| 107 |
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.